//
//  UIButton+QLXExt.m
//  fcuhConsumer
//
//  Created by 邱良雄 on 15/8/7.
//  Copyright (c) 2015年 avatar. All rights reserved.
//

#import "UIButton+CYExt.h"
#import <objc/runtime.h>

@implementation UIButton(CYExt)
@dynamic actionBlocks;

static NSString *_imageBgNameKey;

- (CGSize)sizeWithFont:(UIFont *)font maxWithd:(float)maxWithd
{
    CGSize maxSize = CGSizeMake(maxWithd, MAXFLOAT);
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self.title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

-(void)setImageBgName:(NSString *)imageBgName
{
    objc_setAssociatedObject(self, &_imageBgNameKey, imageBgName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)imageBgName
{
    return objc_getAssociatedObject(self, &_imageBgNameKey);
}

-(void)exchangeLocationLableImageTitleFont:(UIFont *)font magin:(float)magin{
    // 交换button中title和image的位置
    CGFloat labelWidth = [self sizeWithFont:font maxWithd:MAXFLOAT].width;
    CGFloat imageWith = self.imageView.frame.size.width;
    self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth+magin/2, 0, -labelWidth);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, -imageWith, 0, imageWith+magin/2);
}
//快捷方法
- (void)setHighlightedTitle:(NSString *)highlightedTitle
{
    [self setTitle:highlightedTitle forState:UIControlStateHighlighted];
}

- (NSString *)highlightedTitle
{
    return nil;
}

- (void)setTitleColor:(UIColor *)titleColor
{
    [self setTitleColor:titleColor forState:UIControlStateNormal];
}

- (UIColor *)titleColor
{
    return nil;
}

- (void)setHighlightedTitleColor:(UIColor *)highlightedTitleColor
{
    [self setTitleColor:highlightedTitleColor forState:UIControlStateHighlighted];
}

- (UIColor *)highlightedTitleColor
{
    return nil;
}

- (void)setSelectedTitleColor:(UIColor *)selectedTitleColor
{
    [self setTitleColor:selectedTitleColor forState:UIControlStateSelected];
}

- (UIColor *)selectedTitleColor
{
    return nil;
}

- (void)setTitle:(NSString *)title
{
    //self.title = xxx 的时候会调用
    [self setTitle:title forState:UIControlStateNormal];
}

- (NSString *)title
{
    //self.title 的时候会调用
    return [self titleForState:UIControlStateNormal];
}

- (void)setSelectedTitle:(NSString *)selectedTitle
{
    [self setTitle:selectedTitle forState:UIControlStateSelected];
}

- (NSString *)selectedTitle
{
    return [self titleForState:UIControlStateSelected];
}

- (void)setImage:(NSString *)image
{
    [self setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
}

- (NSString *)image
{
    return nil;
}

- (void)setHighlightedImage:(NSString *)image
{
    [self setImage:[UIImage imageNamed:image] forState:UIControlStateHighlighted];
}

- (NSString *)highlightedImage
{
    return nil;
}

- (void)setSelectedImage:(NSString *)image
{
    [self setImage:[UIImage imageNamed:image] forState:UIControlStateSelected];
}

- (NSString *)selectedImage
{
    return nil;
}

- (void)setBgImage:(NSString *)image
{
    [self setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
}

- (NSString *)bgImage
{
    return nil;
}

- (void)setHighlightedBgImage:(NSString *)image
{
    [self setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateHighlighted];
}

- (NSString *)highlightedBgImage
{
    return nil;
}

- (void)setSelectedBgImage:(NSString *)image
{
    [self setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateSelected];
}

- (NSString *)selectedBgImage
{
    return nil;
}

- (void)addTarget:(id)target action:(SEL)action
{
    [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

-(UIFont *)titleFont
{
    return self.titleLabel.font;
}

-(void)setTitleFont:(UIFont *)titleFont
{
    self.titleLabel.font = titleFont;
}

-(CGSize)realBtnSize
{
    CGSize titleSize = [self sizeWithFont:self.titleLabel.font maxWithd:[UIScreen mainScreen].bounds.size.width];
    
    CGFloat realWith = titleSize.width+self.imageView.image.size.width+self.titleEdgeInsets.left+self.titleEdgeInsets.right+self.imageEdgeInsets.left+self.imageEdgeInsets.right;
    CGFloat realHeight = self.imageView.image.size.height>titleSize.height?self.imageView.image.size.height:titleSize.height;
    
    //kAutoKScreenWidth(1)是补充算的按比例不一定准的误差
    return CGSizeMake(realWith+1, realHeight+1);
}

//网络请求图片数据(头像)
//- (void)setHeader:(NSString *)headerUrl
//{
//    UIImage *placeholder = [UIImage imageNamed:@"bg_tx2"];
//    [self setImage:placeholder forState:UIControlStateNormal];
//    [self sd_setImageWithURL:[NSURL URLWithString:headerUrl] forState:UIControlStateNormal placeholderImage:placeholder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//        // 图片下载失败，直接返回，按照它的默认做法
//        if (!image) return;
//        [self setImage:[image xmg_circleImage] forState:UIControlStateNormal];
//    }];
//}
//
////正常图片
//- (void)setImageUrl:(NSString *)url
//{
//    UIImage *placeholder = [UIImage imageNamed:@"bg_default1"];
//    [self setImage:placeholder forState:UIControlStateNormal];
//    [self sd_setImageWithURL:[NSURL URLWithString:url] forState:UIControlStateNormal placeholderImage:placeholder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//        // 图片下载失败，直接返回，按照它的默认做法
//        if (!image) return;
//        [self setImage:image forState:UIControlStateNormal];
//    }];
//}


/**------------------------------------------------------------------------------------*/
+(instancetype) createWithFrame:(CGRect) frame{
    return  [[self alloc] initWithFrame:frame];
}

+(instancetype) create{
    
    return  [self buttonWithType:(UIButtonTypeCustom)];
}

+(instancetype) createWithText:(NSString *)text{
    UIButton * button = [self create];
    [button setTitle:text forState:(UIControlStateNormal)];
    return button;
}

+(instancetype) createWithText:(NSString *)text color:(UIColor *)color font:(UIFont *) font
{
    UIButton * button = [self createWithText:text];
    [button setTitleFont:font];
    [button setTitleColor:color forState:UIControlStateNormal];
    return button;
}

+(instancetype) createWithText:(NSString *)text color:(UIColor *)color font:(UIFont *) font normal:(NSString *) normal
{
    return [self createWithText:text color:color hldColor:color font:font normal:normal highlighted:nil];
}
+(instancetype) createWithText:(NSString *)text color:(UIColor *)color sldColor:(UIColor *)sldColor font:(UIFont *) font normal:(NSString *) normal
{
    return [self createWithText:text color:color sldColor:sldColor font:font normal:normal sld:nil];
}
+(instancetype) createWithText:(NSString *)text color:(UIColor *)color hldColor:(UIColor *)hldColor font:(UIFont *)font normal:(NSString *) normal highlighted:(NSString *) highlighted
{
    UIButton * button = [self createWithText:text color:color font:font];
    [button setImage:[UIImage imageNamed:normal] forState:UIControlStateNormal];
    button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [button setTitleColor:hldColor forState:UIControlStateHighlighted];
    if (highlighted) {
        [button setImage:[UIImage imageNamed:highlighted] forState:UIControlStateHighlighted];
    }
    return button;
}
+(instancetype) createWithText:(NSString *)text color:(UIColor *)color sldColor:(UIColor *)sldColor font:(UIFont *) font normal:(NSString *) normal sld:(NSString *) sld
{
    UIButton *button = [self createWithText:text color:color hldColor:color font:font normal:normal highlighted:nil];
    [button setImage:[UIImage imageNamed:sld] forState:UIControlStateSelected];
    [button setTitleColor:sldColor forState:UIControlStateSelected];
    return button;
}

/**纯图片*/
+(instancetype) createWithNormalBgImageStr:(NSString *)str
{
    return [self createWithNormalBgImageStr:str hightStr:nil];
}
+(instancetype) createWithNormalBgImageStr:(NSString *)str hightStr:(NSString *)hightStr
{
    UIButton * button = [self create];
    [button setBackgroundImage:[UIImage imageNamed:str] forState:UIControlStateNormal];
    if (hightStr) {
        [button setBackgroundImage:[UIImage imageNamed:str] forState:UIControlStateHighlighted];
    }
    return button;
}
+(instancetype) createWithNormalBgImageStr:(NSString *)str sldStr:(NSString *)sldStr
{
    UIButton * button = [self create];
    
    [button setBackgroundImage:[UIImage imageNamed:str] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:sldStr] forState:UIControlStateSelected];
    return button;
}

/**纯图片 视觉小 事实上大*/
+(instancetype) createWithNormalImageStr:(NSString *)str
{
    return [self createWithNormalImageStr:str hightStr:nil];
}
+(instancetype) createWithNormalImageStr:(NSString *)str hightStr:(NSString *)hightStr
{
    UIButton * button = [self create];
    [button setImage:[UIImage imageNamed:str] forState:UIControlStateNormal];
    if (hightStr) {
        [button setImage:[UIImage imageNamed:str] forState:UIControlStateHighlighted];
    }
    return button;
}
+(instancetype) createWithNormalImageStr:(NSString *)str sldStr:(NSString *)sldStr
{
    UIButton * button = [self createWithNormalImageStr:str hightStr:nil];
    [button setImage:[UIImage imageNamed:str] forState:UIControlStateSelected];
    return button;
}

static char ActionBlocksKey;

-(NSMutableArray *)actionBlocks{
    NSMutableArray * ab = objc_getAssociatedObject(self, &ActionBlocksKey);
    if (!ab) {
        ab = [NSMutableArray new];
        self.actionBlocks = ab;
    }
    return ab;
}

-(void)setActionBlocks:(NSMutableArray *)actionBlocks{
    objc_setAssociatedObject(self, &ActionBlocksKey, actionBlocks, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(void) addClickActionBlock:(ButtonActionClorce) block{
    [self.actionBlocks addObject:block];
}


-(void)onEnter{
    //[super onEnter];
    [self addTarget:self action:@selector(onClick:) forControlEvents:(UIControlEventTouchUpInside)];
}

-(void) onClick:(UIButton *)sender{
    for (ButtonActionClorce block in self.actionBlocks) {
        if (block) {
            block(sender);
        }
    }
}


@end
